let gulp = require('gulp');
let less = require('gulp-less');
let concat = require('gulp-concat');
let rename = require('gulp-rename');
let clean = require('gulp-clean-css');
let uglify = require('gulp-uglify');
let autoprefixer = require('gulp-autoprefixer');
let moment = require('moment');
let htmlmin = require('gulp-htmlmin');
let path = require('path');

let destPath = path.join(__dirname, '../lvzhoujingling-dist');

//合并压缩
let concatJsName = 'common-build.js';
//需要压缩的文件,按照顺序放好
let concatJsList = [
	'js/libs/mui.min.js',
	'js/libs/vue.min.js',
	'js/libs/vue-i18n.js',
	'js/libs/mui.pullToRefresh.js',
	'js/libs/mui.pullToRefresh.material.js',
	'i18n/base.js',
	'js/common/app-config.js',
	'js/common/common-tool.js',
	'js/common/app-data-manager.js',
	'js/common/base-request.js',
	'js/common/utils.js',
	'js/element/toggle.js'
];

/**
 * less编译压缩
 */
gulp.task('less', function() {
	return gulp.src(['less/style.less'])
		.pipe(less()) //   less编译
		.pipe(autoprefixer()) //  公共前缀
		.pipe(gulp.dest('css/'));
});
/**
 * 执行合并压缩的任务  gulp uglify
 */
gulp.task('uglify', function() {
	console.info('[' + moment().format('LLL') + '] 正在压缩合并公共js');
	console.info(concatJsList.map(function(s, i) {
		return(i + 1) + ':' + s;
	}).join('\n'));
	return gulp.src(concatJsList)
		.pipe(concat(concatJsName, {
			newLine: ';'
		}))
		.pipe(uglify({
			mangle: {
				//  下面的关键字不混淆，否则会导致调用失败
				except: ['Vue', 'VueI18n', 'mui', 'window', '$']
			}
		}))
		.pipe(gulp.dest('./js')).on('end', function() {
			console.info('[' + moment().format('LLL') + '] 公共js合并完成');
		});
});

/**
 * 监控文件变化
 */
gulp.task('watch', function() {
	gulp.watch('less/*.less', ['less']);
});

//执行默认的任务
gulp.task('default', ['less', 'watch']);